// Job
// 
// 保佑没Bug 
//
// Created by AS on 2019/5/5. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "JobListViewController.h"
#import "JoBDetailsViewController.h"
#import "JobTableViewCell.h"
@interface JobListViewController ()

@end

@implementation JobListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self.tableView registerNib:[UINib nibWithNibName:@"JobTableViewCell" bundle:nil] forCellReuseIdentifier:@"JobTableViewCell"];
    [self.tableView.mj_header beginRefreshing];
    
    

    

    
}



-(void)down_refreshData{
    self.page = 0;
    [self getJobs:YES];
}

-(void)loadMoreData{
    self.page ++;
    [self getJobs:NO];
}

- (void)getJobs:(Boolean)isRefresh {
    LocationManager *manager = [LocationManager manager];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"start_index"] = @(self.page);
    dict[@"page_size"] = @"20";
    dict[@"category_id"] = self.category_id;
    dict[@"city"] = manager.location;
    
    [HTTPTools GET:getJobListUrl parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (!error) {
            if (!msg) {
                if (isRefresh) {
                    [self.dataSource removeAllObjects];
                }
                NSArray *array = [XLJobModel mj_objectArrayWithKeyValuesArray:result];
                [self.dataSource addObjectsFromArray:array];
                
                if (array.count < 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.tableView.mj_footer resetNoMoreData];
                }
                
                if (self.dataSource.count <= 0) {
                    [self showNoDataViewWithMessage:@"没有内容!"];
                }else{
                    [self hideNoDataView];
                }
                
                [self reloadData];
            }
        }
    }];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JobTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JobTableViewCell" forIndexPath:indexPath];
    XLJobModel *model = self.dataSource[indexPath.row];
    [cell setModel:model withHideIcon:YES];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JoBDetailsViewController *details = [JoBDetailsViewController new];
    details.model = self.dataSource[indexPath.row];
    [self pushViewController:details];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.0;
}

@end
